<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设计模式精解：工厂模式与抽象工厂模式</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        .noto-serif {
            font-family: 'Noto Serif SC', serif;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.9;
            margin-right: 0.2em;
            margin-bottom: -0.1em;
            color: #4f46e5;
            font-weight: 700;
        }
        .code-block {
            background-color: #282c34;
            color: #abb2bf;
            border-radius: 8px;
            overflow: hidden;
        }
        .code-header {
            background-color: #1e2127;
            color: #9da5b4;
            padding: 0.5rem 1rem;
            font-family: monospace;
            display: flex;
            justify-content: space-between;
        }
        .code-body {
            padding: 1rem;
            font-family: monospace;
            white-space: pre;
            overflow-x: auto;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .gradient-text {
            background: linear-gradient(90deg, #4f46e5, #9333ea);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        .hero-pattern {
            background-color: #f9fafb;
            background-image: radial-gradient(#e5e7eb 1px, transparent 1px);
            background-size: 20px 20px;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-pattern py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 noto-serif gradient-text">深入理解设计模式</h1>
            <h2 class="text-2xl md:text-3xl font-semibold text-gray-700 mb-8 noto-serif">工厂模式与抽象工厂模式的精要解析</h2>
            <div class="max-w-3xl mx-auto">
                <p class="text-xl text-gray-600 mb-8 drop-cap">在面向对象编程中，工厂模式与抽象工厂模式是两种常用的创建型设计模式。它们都能帮助我们实现对象的创建与使用的分离，但各自有着不同的应用场景和实现方式。本文将深入探讨这两种模式的异同，帮助您在项目中做出更明智的选择。</p>
                <div class="flex justify-center space-x-4">
                    <a href="#factory-pattern" class="px-6 py-3 bg-indigo-600 text-white font-medium rounded-lg hover:bg-indigo-700 transition duration-300">
                        <i class="fas fa-industry mr-2"></i>工厂模式
                    </a>
                    <a href="#abstract-factory" class="px-6 py-3 bg-purple-600 text-white font-medium rounded-lg hover:bg-purple-700 transition duration-300">
                        <i class="fas fa-factory mr-2"></i>抽象工厂模式
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Comparison Diagram -->
    <section class="py-16 px-4 md:px-0 bg-white">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold text-center mb-12 noto-serif">模式对比关系图</h2>
            <div class="mermaid">
                graph TD
                    A[创建型设计模式] --> B[工厂模式]
                    A --> C[抽象工厂模式]
                    
                    B --> D[简单工厂模式]
                    B --> E[工厂方法模式]
                    
                    D -->|创建单一产品| F[一个工厂类]
                    D -->|静态方法| G[根据参数返回不同实例]
                    
                    E -->|创建相关产品| H[工厂接口]
                    E -->|子类实现| I[不同工厂方法]
                    
                    C -->|创建系列产品| J[抽象工厂接口]
                    C -->|多个工厂方法| K[创建整套相关产品]
                    C -->|确保兼容性| L[同一产品族]
            </div>
        </div>
    </section>

    <main class="container mx-auto max-w-5xl py-12 px-4 md:px-0">
        <!-- Factory Pattern Section -->
        <section id="factory-pattern" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                    <i class="fas fa-industry text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold noto-serif">工厂模式</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-6 rounded-xl shadow-md transition duration-300 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center mr-3">
                            <i class="fas fa-star text-indigo-500 text-sm"></i>
                        </div>
                        <h3 class="text-xl font-semibold">简单工厂模式</h3>
                    </div>
                    <p class="text-gray-600 mb-4">由一个工厂类根据提供的信息生成对象实例。通常，工厂类通过一个静态方法来创建对象，根据不同的输入参数返回不同的实例。</p>
                    <div class="code-block rounded-lg overflow-hidden">
                        <div class="code-header">
                            <span>Java 示例</span>
                            <button class="text-xs hover:text-indigo-400">
                                <i class="far fa-copy mr-1"></i>复制
                            </button>
                        </div>
                        <div class="code-body">
public class SimpleFactory {
    public static Product createProduct(String type) {
        switch (type) {
            case "A": return new ProductA();
            case "B": return new ProductB();
            default: throw new IllegalArgumentException("Unknown type");
        }
    }
}
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md transition duration-300 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center mr-3">
                            <i class="fas fa-project-diagram text-indigo-500 text-sm"></i>
                        </div>
                        <h3 class="text-xl font-semibold">工厂方法模式</h3>
                    </div>
                    <p class="text-gray-600 mb-4">定义了一个用于创建对象的接口，但由子类来决定具体实现类。每个子类都实现自己的工厂方法来创建相应的对象。</p>
                    <div class="code-block rounded-lg overflow-hidden">
                        <div class="code-header">
                            <span>Java 示例</span>
                            <button class="text-xs hover:text-indigo-400">
                                <i class="far fa-copy mr-1"></i>复制
                            </button>
                        </div>
                        <div class="code-body">
public interface Product {
    void use();
}

public class ProductA implements Product {
    public void use() {
        // Implementation
    }
}

public interface Factory {
    Product createProduct();
}

public class FactoryA implements Factory {
    public Product createProduct() {
        return new ProductA();
    }
}
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md mb-8">
                <h3 class="text-2xl font-semibold mb-4 noto-serif">工厂模式特点</h3>
                <ul class="space-y-4">
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-indigo-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>创建的对象：</strong>简单工厂模式通常创建单一产品的不同实例，工厂方法模式通常创建一系列相关的产品</span>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-indigo-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>对象相关性：</strong>工厂方法模式中工厂负责创建一类相关的对象，但不涉及更多产品系列</span>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-indigo-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-indigo-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>扩展性：</strong>通常通过增加新的产品类型(简单工厂)或增加新的工厂子类(工厂方法)来扩展</span>
                    </li>
                </ul>
            </div>
        </section>
        
        <!-- Abstract Factory Section -->
        <section id="abstract-factory" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                    <i class="fas fa-factory text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold noto-serif">抽象工厂模式</h2>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md mb-8">
                <h3 class="text-2xl font-semibold mb-4 noto-serif">核心概念</h3>
                <p class="text-gray-600 mb-6">抽象工厂模式提供一个接口，用于创建一系列相关或相互依赖的对象，而无需指定具体的类。通常，抽象工厂模式包含多个工厂方法，每个方法负责创建一种产品。</p>
                
                <div class="code-block rounded-lg overflow-hidden mb-8">
                    <div class="code-header">
                        <span>Java 示例</span>
                        <button class="text-xs hover:text-purple-400">
                            <i class="far fa-copy mr-1"></i>复制
                        </button>
                    </div>
                    <div class="code-body">
public interface AbstractFactory {
    ProductA createProductA();
    ProductB createProductB();
}

public class ConcreteFactory1 implements AbstractFactory {
    public ProductA createProductA() {
        return new ProductA1();
    }
    public ProductB createProductB() {
        return new ProductB1();
    }
}
                    </div>
                </div>
                
                <h3 class="text-2xl font-semibold mb-4 noto-serif">抽象工厂模式特点</h3>
                <ul class="space-y-4">
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-purple-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-purple-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>创建的对象：</strong>创建一系列相关的产品对象，通常涉及多个产品类。一个抽象工厂可以创建一整套相关的产品</span>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-purple-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-purple-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>对象相关性：</strong>强调对象的相关性。确保创建的一系列产品在同一个系列中彼此兼容</span>
                    </li>
                    <li class="flex items-start">
                        <div class="flex-shrink-0 h-6 w-6 rounded-full bg-purple-100 flex items-center justify-center mr-3 mt-1">
                            <i class="fas fa-check text-purple-600 text-xs"></i>
                        </div>
                        <span class="text-gray-700"><strong>扩展性：</strong>通常通过增加新的产品系列和相应的具体工厂来扩展。添加新产品系列时，可能需要创建新的工厂实现和新的产品实现</span>
                    </li>
                </ul>
            </div>
        </section>
        
        <!-- Summary Section -->
        <section class="bg-gradient-to-r from-indigo-50 to-purple-50 p-8 rounded-xl shadow-md">
            <h2 class="text-3xl font-bold mb-6 text-center noto-serif">模式选择指南</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-check-circle text-indigo-500 mr-2"></i> 选择工厂模式
                    </h3>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-indigo-400 mt-1 mr-2"></i>
                            <span>只需要创建单一类型的产品或产品的不同变体</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-indigo-400 mt-1 mr-2"></i>
                            <span>系统不关心如何创建对象，只需要获得能够使用的对象</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-indigo-400 mt-1 mr-2"></i>
                            <span>对象的创建过程相对简单，不需要复杂的初始化逻辑</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fas fa-check-circle text-purple-500 mr-2"></i> 选择抽象工厂模式
                    </h3>
                    <ul class="space-y-3 text-gray-600">
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-purple-400 mt-1 mr-2"></i>
                            <span>需要创建多个相关或依赖的产品族</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-purple-400 mt-1 mr-2"></i>
                            <span>系统需要保证使用同一产品族中的产品</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-angle-right text-purple-400 mt-1 mr-2"></i>
                            <span>产品类的结构可能变化，但接口稳定</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col items-center">
                <h3 class="text-xl font-medium mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-300 hover:text-white transition duration-300">
                    <i class="fas fa-link mr-1"></i> www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            themeVariables: {
                primaryColor: '#e5e7eb',
                primaryBorderColor: '#4f46e5',
                primaryTextColor: '#111827',
                lineColor: '#4f46e5',
                fontSize: '14px'
            }
        });

        // Copy code blocks
        document.querySelectorAll('.code-header button').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.parentElement.nextElementSibling;
                const range = document.createRange();
                range.selectNode(codeBlock);
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                const originalText = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check mr-1"></i>已复制';
                setTimeout(() => {
                    button.innerHTML = originalText;
                }, 2000);
            });
        });
    </script>
</body>
</html>